package com.example.demo.controller;

import com.example.demo.dto.PieDto;
import com.example.demo.entity.*;
import com.example.demo.service.FoodService;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/food")
public class FoodController {
    @Autowired
    private FoodService foodService;
    @RequestMapping("/show_food_page")
    public String show_food_page(Model model){
        //商家数量
        int food_num=0;
        List<FoodInfo> foodInfos=foodService.findFoodInfo();
        List<FoodTypeInfo> foodTypeInfos=foodService.findFoodTypeInfo();
        List<FoodAreaInfo> foodAreaInfos=foodService.findFoodAreaInfo();
        List<FoodPointRank> foodPointRanks=foodService.findFoodPointRank();
        List<FoodPopularityRank> foodPopularityRankList=foodService.findFoodPopularityRank();
        //人均消费
        double food_average_money =0;
        double money=0;
        //平均评分
        double food_average_point=0;
        double points=0;
        //种类数量
        int food_type_num=0;
        //包含地区
        int food_area_num=0;
        //最火类型
        String food_hot_type=null;
        food_num=foodInfos.size();
        for (int i=0;i<food_num;i++){
            FoodInfo foodInfo =foodInfos.get(i);
            money=money+foodInfo.getAverage();
            points=points+foodInfo.getPoint();
        }
        food_average_money=money/food_num;
        BigDecimal a = new BigDecimal(food_average_money);
        food_average_money=a.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
        food_average_point=points/food_num;
        BigDecimal b = new BigDecimal(food_average_point);
        food_average_point=b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
        food_type_num=foodTypeInfos.size();
        food_area_num=foodAreaInfos.size();
        FoodTypeInfo foodTypeInfoMax=foodTypeInfos.get(0);
        for (int i=0;i<food_type_num;i++){
            FoodTypeInfo foodTypeInfodb=foodTypeInfos.get(i);
            if (foodTypeInfodb.getNum()>foodTypeInfoMax.getNum()){
                foodTypeInfoMax=foodTypeInfodb;
            }
        }
        food_hot_type=foodTypeInfoMax.getType();
        model.addAttribute("food_num",food_num);
        model.addAttribute("food_average_money",food_average_money);
        model.addAttribute("food_average_point",food_average_point);
        model.addAttribute("food_type_num",food_type_num);
        model.addAttribute("food_area_num",food_area_num);
        model.addAttribute("food_hot_type",food_hot_type);
        model.addAttribute("foodPointRanks",foodPointRanks.subList(0,20));
        model.addAttribute("foodPopularityRankList",foodPopularityRankList.subList(0,20));

        return "food";
    }
    @RequestMapping("/foodInfo")
    public ModelAndView foodInfo(ModelAndView modelAndView,@RequestParam(defaultValue="1")Integer
            pageNum,@RequestParam(defaultValue="15")Integer pageSize){
        PageInfo<FoodInfo> foodInfoPageInfo=foodService.selectFoodPage(pageNum,pageSize);
        modelAndView.addObject("foodInfoPageInfo",foodInfoPageInfo);
        modelAndView.setViewName("foodInfo");
        return modelAndView;
    }
    //美食饼图
    @RequestMapping("/food_echarts_1")
    @ResponseBody
    public List<PieDto> food_echarts_1(){
        List<PieDto> pieDtos=new ArrayList<>();
        List<FoodTypeInfo> foodTypeInfo= foodService.findFoodTypeInfo();
        for (int i =0;i<foodTypeInfo.size();i++){
            FoodTypeInfo foodTypeInfodb=foodTypeInfo.get(i);
            PieDto pieDtodb=new PieDto();
            pieDtodb.setName(foodTypeInfodb.getType());
            pieDtodb.setValue(foodTypeInfodb.getNum());
            pieDtos.add(pieDtodb);
        }
        System.out.println(pieDtos.toString());
        return pieDtos;

    }

    @RequestMapping("/food_echarts_2_v")
    @ResponseBody
    public List<List<Double>> food_echarts_2_v(){
        List<List<Double>> vlists=new ArrayList<>();
        List<Double> list1=new ArrayList<>();
        List<Double> list2=new ArrayList<>();
        List<FoodTypeInfo> foodTypeInfos=foodService.findFoodTypeInfo();
        for(int i =0;i<foodTypeInfos.size();i++){
            FoodTypeInfo foodTypeInfo=foodTypeInfos.get(i);
            list1.add((double) foodTypeInfo.getNum());
            list2.add(foodTypeInfo.getAverage());
        }
        vlists.add(list1);
        vlists.add(list2);
        return vlists;

    }
    @RequestMapping("/food_echarts_2_x")
    @ResponseBody
    public List<String> food_echarts_2_x(){
        List<String> xlist=new ArrayList<>();
        List<FoodTypeInfo> foodTypeInfos=foodService.findFoodTypeInfo();
        for (int i =0;i<foodTypeInfos.size();i++){
            FoodTypeInfo foodTypeInfo=foodTypeInfos.get(i);
            xlist.add(foodTypeInfo.getType());
        }
        return xlist;
    }
    @RequestMapping("/food_echarts_3")
    @ResponseBody
    public List<PieDto> food_echarts_3(){
        List<PieDto> pieDtos=new ArrayList<>();
        List<FoodAreaInfo> foodAreaInfos=foodService.findFoodAreaInfo();
        for(int i=0;i<foodAreaInfos.size();i++){
            FoodAreaInfo foodAreaInfo = foodAreaInfos.get(i);
            PieDto pieDtodb=new PieDto();
            pieDtodb.setName(foodAreaInfo.getArea());
            pieDtodb.setValue(foodAreaInfo.getNum());
            pieDtos.add(pieDtodb);
        }
        System.out.println(pieDtos.toString());
        return pieDtos;
    }
    @RequestMapping("/insertFood")
    public String insertFood(Model model){
        List<FoodTypeInfo> foodTypeInfos=foodService.findFoodTypeInfo();
        List<FoodAreaInfo> foodAreaInfos=foodService.findFoodAreaInfo();
        model.addAttribute("type",foodTypeInfos);
        model.addAttribute("area",foodAreaInfos);
        return "insertFood";
    }
    @RequestMapping("/doinsertFood")
    @ResponseBody
    public Map<String,Object> doinsertFood(FoodInfo foodInfo){
        Map<String,Object> resultMap = new HashMap<String, Object>();
       FoodInfo foodInfodb= foodService.findFoodInfoByname(foodInfo.getName());
       if (foodInfodb!=null){
           resultMap.put("result","该家店已经存在啦");
       }else {
           foodService.insertFood(foodInfo);
           resultMap.put("result","SUCCESS");
       }
       return resultMap;

    }
    @RequestMapping("/updateFood")
    public String updateFood(Model model,int id){
        FoodInfo foodInfo=foodService.findFoodById(id);
        List<FoodTypeInfo> foodTypeInfos=foodService.findFoodTypeInfo();
        List<FoodAreaInfo> foodAreaInfos=foodService.findFoodAreaInfo();
        model.addAttribute("type",foodTypeInfos);
        model.addAttribute("area",foodAreaInfos);
        model.addAttribute("food",foodInfo);
        return "updateFood";
    }
    @RequestMapping("/doupdateFood")
    @ResponseBody
    public Map<String,Object> doupdateFood(FoodInfo foodInfo){
        Map<String,Object> resultMap = new HashMap<String, Object>();
        FoodInfo foodInfodb=foodService.findFoodInfoByname(foodInfo.getName());
        if (foodInfodb!=null&&foodInfodb.getId()!=foodInfo.getId()){
            resultMap.put("result","该家店已经存在啦");
        }
        else {
            FoodInfo foodInfo1=foodService.findFoodById(foodInfo.getId());
            foodService.updateFoodEX(foodInfo1);
            foodService.updateFood(foodInfo);
            resultMap.put("result","SUCCESS");
        }
        return resultMap;
    }
    @RequestMapping("/deleteFood")
    @ResponseBody
    public Map<String,Object> deleteFood(int id){
        Map<String,Object> resultMap = new HashMap<String, Object>();
        foodService.deleteFood(id);
        resultMap.put("result","SUCCESS");
        return resultMap;

    }

}
